package cn.com.xjnt.vas.web.ordering.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.frameworks.core.logger.annotation.RunLogger;
import com.frameworks.core.web.controller.BaseController;
import com.frameworks.core.web.page.WebPage;
import com.frameworks.core.web.result.DataResult;
import com.frameworks.core.web.result.MsgResult;
import com.frameworks.core.web.search.DynamicSpecifications;
import com.google.common.collect.Lists;

import cn.com.xjnt.vas.ordering.domain.entity.Notice;
import cn.com.xjnt.vas.ordering.domain.vo.NoticeVo;
import cn.com.xjnt.vas.ordering.service.NoticeService;

/**
 * 类说明：
 * 
 * @author 作者 LUXIN
 * @version 创建时间：2016年5月11日17:49:50
 */
@Controller
@RequestMapping("/manage/ordering/notice")
public class NoticeController extends BaseController {

    private static final String VIEW_PAGE = "ordering/notice/view";
    private static final String FORM_PAGE = "ordering/notice/form";

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private Mapper mapper;

    protected NoticeService getService() {
        return noticeService;
    }

    /**
     * view页面
     * 
     * @param request
     * @return
     */
    @RequiresPermissions(value = { "Order:notice:show" })
    @RequestMapping(value = "", method = { RequestMethod.GET })
    public ModelAndView show(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView(VIEW_PAGE);
        mav.addObject("organizationId",getLoginUser().getOrganization().getId());//数据权限
        return mav;
    }
    
    /**
     * view页面list方法
     * 
     * @param request
     * @param webPage
     * @return
     */
    @RequiresPermissions(value = { "Order:notice:show" })
    @RequestMapping(value = "/list", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public DataResult list(HttpServletRequest request, WebPage webPage) {
        DataResult dataRet = new DataResult();
        try {
            Specification<Notice> specification = DynamicSpecifications.bySearchFilter(request, Notice.class);
            List<Notice> models = getService().findByCondition(specification, webPage);
            List<NoticeVo> lstVo = Lists.newArrayList();
            for (Notice model : models) {
                NoticeVo vo = mapper.map(model, NoticeVo.class);
                lstVo.add(vo);
            }
            dataRet.pushOk("获取数据列表成功！");
            dataRet.setTotal(webPage.getTotalCount());
            dataRet.setRows(lstVo);
        } catch (Exception e) {
            dataRet.pushError("获取数据列表失败！");
            logger.error("获取数据列表异常！", e);
        }
        return dataRet;
    }

    /**
     * 新增
     * 
     * @param request
     * @return
     */
    @RequiresPermissions(value = { "Order:notice:show" })
    @RequestMapping(value = "/add", method = { RequestMethod.GET })
    public ModelAndView add(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView(FORM_PAGE);
        mav.addObject("organizationId",getLoginUser().getOrganization().getId());//数据权限
        return mav;
    }

    /**
     * 修改
     * 
     * @param id
     * @return
     */
    @RequiresPermissions(value = { "Order:notice:show" })
    @RequestMapping(value = "/edit/{id}", method = { RequestMethod.GET })
    public ModelAndView edit(@PathVariable("id") Long id) {
        ModelAndView mav = new ModelAndView(FORM_PAGE);
        Notice model = getService().findById(id);

        mav.addObject("notice", model);
        mav.addObject("organizationId",getLoginUser().getOrganization().getId());//数据权限
        return mav;
    }

    /**
     * 保存
     * 
     * @param entity
     * @return
     */
    @RunLogger(value = "添加公告", isSaveRequest = true)
    @RequiresPermissions(value = { "Order:notice:add" })
    @RequestMapping(value = "/add", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public MsgResult store(Notice entity) {
        MsgResult msgRet = new MsgResult();
        try {
            entity.setStatus(0);//未审核
            getService().save(entity);
            msgRet.pushOk("添加成功！");
        } catch (Exception e) {
            msgRet.pushError("添加失败：" + e.getMessage());
            logger.error("添加时，发生异常！", e);
        }
        return msgRet;
    }

    /**
     * 更新
     * 
     * @param entity
     * @return
     */
    @RunLogger(value = "编辑公告", isSaveRequest = true)
    @RequiresPermissions(value = { "Order:notice:modify" })
    @RequestMapping(value = "/edit", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public MsgResult modify(Notice entity) {
        MsgResult msgRet = new MsgResult();
        try {
            entity.setStatus(0);//修改后，需要重新审核
            entity.setCreateTime(getService().findById(entity.getId()).getCreateTime());
            getService().update(entity);
            msgRet.pushOk("修改成功!");
        } catch (Exception e) {
            msgRet.pushError("修改失败：" + e.getMessage());
            logger.error("修改时，发生异常！", e);
        }
        return msgRet;
    }

    /**
     * 删除
     * 
     * @param id
     * @return
     */
    @RunLogger(value = "删除公告", isSaveRequest = true)
    @RequiresPermissions(value = { "Order:notice:remove" })
    @RequestMapping(value = "/remove/{id}", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public MsgResult remove(@PathVariable("id") Long id) {
        MsgResult msgRet = new MsgResult();
        try {
            getService().deleteById(id);
            msgRet.pushOk("删除成功！");
        } catch (Exception e) {
            msgRet.pushError("删除失败：" + e.getMessage());
            logger.error("删除时，发生异常！", e);
        }
        return msgRet;
    }

    /**
     * 批量删除
     * 
     * @param ids
     * @return
     */
    @RunLogger(value = "批量删除公告", isSaveRequest = true)
    @RequiresPermissions(value = { "Order:notice:remove" })
    @RequestMapping(value = "/remove", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public MsgResult remove(@RequestParam("ids[]") Long[] ids) {
        MsgResult msgRet = new MsgResult();
        try {
            getService().deleteByIds(ids);
            msgRet.pushOk("批量删除成功!");
        } catch (Exception e) {
            msgRet.pushError("批量删除失败：" + e.getMessage());
            logger.error("批量删除时，发生异常！", e);
        }
        return msgRet;
    }
    
    /**
     * 审核
     * 
     * @param id
     * @return
     */
    @RunLogger(value = "审核公告", isSaveRequest = true)
    @RequiresPermissions(value = { "Order:notice:examine" })
    @RequestMapping(value = "/examine/{id}", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public MsgResult examine(@PathVariable("id") Long id) {
        MsgResult msgRet = new MsgResult();
        try {
            getService().examineById(id);
            msgRet.pushOk("审核成功！");
        } catch (Exception e) {
            msgRet.pushError("审核失败：" + e.getMessage());
            logger.error("审核时，发生异常！", e);
        }
        return msgRet;
    }
    
    /**
     * 批量审核
     * 
     * @param ids
     * @return
     */
    @RunLogger(value = "批量审核公告", isSaveRequest = true)
    @RequiresPermissions(value = { "Order:notice:examine" })
    @RequestMapping(value = "/examine", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public MsgResult examine(@RequestParam("ids[]") Long[] ids) {
        MsgResult msgRet = new MsgResult();
        try {
            getService().examineByIds(ids);
            msgRet.pushOk("批量审核成功!");
        } catch (Exception e) {
            msgRet.pushError("批量审核失败：" + e.getMessage());
            logger.error("批量审核时，发生异常！", e);
        }
        return msgRet;
    }
    
    /**
     * 停用
     * 
     * @param id
     * @return
     */
    @RunLogger(value = "停用公告", isSaveRequest = true)
    @RequiresPermissions(value = { "Order:notice:disable" })
    @RequestMapping(value = "/disable/{id}", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public MsgResult disable(@PathVariable("id") Long id) {
        MsgResult msgRet = new MsgResult();
        try {
            getService().disableById(id);
            msgRet.pushOk("停用成功！");
        } catch (Exception e) {
            msgRet.pushError("停用失败：" + e.getMessage());
            logger.error("停用时，发生异常！", e);
        }
        return msgRet;
    }
    
    /**
     * 批量停用
     * 
     * @param ids
     * @return
     */
    @RunLogger(value = "批量停用公告", isSaveRequest = true)
    @RequiresPermissions(value = { "Order:notice:disable" })
    @RequestMapping(value = "/disable", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public MsgResult disable(@RequestParam("ids[]") Long[] ids) {
        MsgResult msgRet = new MsgResult();
        try {
            getService().disableByIds(ids);
            msgRet.pushOk("批量停用成功!");
        } catch (Exception e) {
            msgRet.pushError("批量停用失败：" + e.getMessage());
            logger.error("批量停用时，发生异常！", e);
        }
        return msgRet;
    }
}
